/**
 * @file STL_Crypt_PKCS5Padding.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2021-05-11
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2021
 * 
 */

#ifndef __STL_CRYPT_PKCS5_H__
#define __STL_CRYPT_PKCS5_H__

#include <string>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

namespace STL
{
    std::string PKCS5Padding(const std::string &str)
    {
        std::string ret = std::string(str);
        uint8_t size = 8 - ret.size() % 8;
        ret.append(size, size);
        return ret;
    }
    std::string PKCS5UnPadding(const std::string &str)
    {
        if (str.empty())
        {
            return "";
        }
        int size = str.at(str.size() - 1);
        if (size < 0 || size > 8)
        {
            return "";
        }
        if (size > str.size())
        {
            return "";
        }
        return str.substr(0, str.size() - size);
    }
}

#endif //__STL_CRYPT_PKCS5_H__